package br.ucb.tcc.beans;

import java.util.ArrayList;
import java.util.List;

import br.ucb.tcc.dto.TwitterDTO;
import br.ucb.tcc.utils.Constantes;

import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterBean {
	
	public Twitter twitter;
	public TwitterDTO twitterDTO;
	public String buscarPorNome;
	public String twittar;
	public String buscarPorAssunto;

	public void autentica() {

		ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
		configurationBuilder.setOAuthConsumerKey(Constantes.CONSUMER_KEY);
		configurationBuilder.setOAuthConsumerSecret(Constantes.CONSUMER_KEY_SECRET);
		configurationBuilder.setOAuthAccessToken(Constantes.CONSUMER_ACESS_TOKEN);
		configurationBuilder.setOAuthAccessTokenSecret(Constantes.CONSUMER_ACESS_TOKEN_SECRET);
		
		Configuration configuration = configurationBuilder.build();

		try {
			AccessToken accessToken = getTwitter(configuration)
					.getOAuthAccessToken();
			String token = accessToken.getToken();
			String tokenSecret = accessToken.getTokenSecret();

			if (token == null || tokenSecret == null) {
				return;
			}
		} catch (TwitterException e) {
			e.printStackTrace();
			return;
		}
		System.out.println("Autenticou!");
	}

	public Twitter getTwitter(Configuration conf) {
		if (twitter == null) {
			twitter = new TwitterFactory(conf).getInstance();
		}
		return twitter;
	}

	public String enviarMensagem() {
			try {
				autentica();
				Status updateStatus = twitter.updateStatus(twittar);
				System.out.println("Texto Enviado:" + updateStatus.getText());
			} catch (TwitterException e) {
				e.printStackTrace();
		}
			return "/index.faces";
	}

	public List<Tweet> getTweetsPorAssunto() {
		List<Tweet> tweets = null;
		try {
			Query query = new Query(buscarPorAssunto);
			QueryResult queryResult = twitter.search(query);
			tweets = queryResult.getTweets();

		} catch (TwitterException e) {
			e.printStackTrace();
		}
		return tweets;
	}
	
	public List<Status> getListaStatus () {
		try {
			autentica();
			return twitter.getFriendsTimeline();
		} catch (TwitterException e) {
			e.printStackTrace();
		}
		return new ArrayList<Status>();
	}
	
	public String buscarInformacoesPorDeterminadoNome() {
		try {
			User user = null;
			if (buscarPorNome != null && !buscarPorNome.equals("")) {
				user = twitter.showUser(buscarPorNome);
			} else {
				user = twitter.showUser("italoricci");
			}
			setarInformacoesDTO(user);
		} catch (TwitterException e) {
			e.printStackTrace();
		}
		return "/index.faces";
	}
	
	
	public void setarInformacoesDTO (User user) {
		twitterDTO = new TwitterDTO();
		twitterDTO.setNomePerfil(user.getName());
		twitterDTO.setNomeExibicao(user.getScreenName());
		twitterDTO.setUltimoPost(user.getStatus().getText());
	}

	public TwitterDTO getTwitterDTO() {
		return twitterDTO;
	}

	public void setTwitterDTO(TwitterDTO twitterDTO) {
		this.twitterDTO = twitterDTO;
	}

	public Twitter getTwitter() {
		return twitter;
	}

	public void setTwitter(Twitter twitter) {
		this.twitter = twitter;
	}

	public String getBuscarPorNome() {
		return buscarPorNome;
	}

	public void setBuscarPorNome(String buscarPorNome) {
		this.buscarPorNome = buscarPorNome;
	}

	public String getTwittar() {
		return twittar;
	}

	public void setTwittar(String twittar) {
		this.twittar = twittar;
	}

	public String getBuscarPorAssunto() {
		return buscarPorAssunto;
	}

	public void setBuscarPorAssunto(String buscarPorAssunto) {
		this.buscarPorAssunto = buscarPorAssunto;
	}
	
}
